import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // 控制此对象不渲染左侧菜单栏
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: 'Dashboard', icon: 'dashboard' }
      // meta字段固定, 给当前路由对象添加额外的信息
      // title和icon自定义的
      // 前提: 当你切换路由匹配此path路径时, $route的值就会被切换成此对象
    }]
  },

  { path: '*', redirect: '/404', hidden: true }
]

// 创建路由对象的方法
const createRouter = () => new Router({
  // mode: 'history', // 切换路由显示的模式
  scrollBehavior: () => ({ y: 0 }), // 路由切换的时候, 浏览器滚动条表现形式(切换路由滚动条会回到最上面)
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
// 重置路由
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
